package com.gxz.redis.controller;

import com.gxz.redis.domain.UserInfo;
import com.gxz.redis.service.UserInfoService;
import com.gxz.redis.util.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;



/*****
 * @author guxz4
 * @Description: com.itheima.controller.UserInfoController
 ****/
@RestController
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    /****
     * 根据name获取用户信息
     * @param name
     * @return
     */
    @PostMapping(value = "/login")
    public String login(String name) throws Exception{
        //查询用户信息
        UserInfo userInfo = userInfoService.get(name);

        if(userInfo!=null){
            Map<String,Object> userMap = new HashMap<String,Object>();
            userMap.put("username",userInfo.getUsername());
            userMap.put("name",userInfo.getName());
            return JwtTokenUtil.generateTokenUser(UUID.randomUUID().toString(),userMap,1000000000L);
        }
        return "账号或密码错误";
    }

    /****
     * 根据name获取用户信息   http://192.168.211.141/usr/wangwu
     * @param name
     * @return
     */
    @GetMapping(value = "/userinfo/{name}")
    public UserInfo one(@PathVariable(value = "name")String name){
        //try {
        //    System.out.println("hello!");
        //    Thread.sleep(10000);
        //} catch (InterruptedException e) {
        //    e.printStackTrace();
        //}
        return userInfoService.get(name);
    }
}
